//1.阳光类




class SunNum{
    constructor(){
        let s={
            img:null,
            sun_num:window._main.allSunVal,
            x:105,
            y:0,
        }
        Object.assign(this,s)
    }
    static new(){
        let s=new this()
        s.img=imageFromPath(allImg.sunback)
        return s
    }
    draw(cxt){
        let self=this
        cxt.drawImage(self.img,self.x+120,self.y)
        cxt.fillStyle='black'
        cxt.font='24px Microsoft YaHei'
        cxt.fontWeight=700
        cxt.fillText(self.sun_num,self.x+175,self.y+27)
    }
    //修改阳光 !!!!!
    changeSunNum(num=25){
        let self=this
        window._main.allSunVal+=num
        self.sun_num+=num
    }
}
// 2.卡片类
class Card{
    constructor(obj){
        let c={
            name:obj.name,
            canGrow:true,
            canClick:true,
            img:null,
            images:[],
            timer:null,
            timer_spacing:obj.timer_spacing,
            timer_num:1,
            sun_val:obj.sun_val,
            row:obj.row,
            x:0,
            y:obj.y,
        }
        Object.assign(this,c)
    }
    static new(obj){
        let b=new this(obj)
        b.images.push(imageFromPath(allImg.plantsCard[b.name].img))       
        b.images.push(imageFromPath(allImg.plantsCard[b.name].imgG)) 
        if(b.canClick){
            b.img=b.images[0]
        }else{
            b.img=b.images[1]
        }
        b.timer_num = b.timer_spacing / 1000  //1000ms                           
        return b
    }
    draw(cxt) {
        let self = this, marginLeft = 120
        if(self.sun_val > window._main.allSunVal){
            self.canGrow = false
        }else{
            self.canGrow = true
        }
        if(self.canGrow && self.canClick){
            self.img = self.images[0]
        }else{
            self.img = self.images[1]
        }

        cxt.drawImage(self.img, self.x + marginLeft, self.y)

        cxt.fillStyle = 'black'
        cxt.font = '16px Microsoft YaHei'
        cxt.fillText(self.sun_val, self.x + marginLeft + 60, self.y + 55)
        if (!self.canClick && self.canGrow) {
            cxt.fillStyle = 'rgb(255, 255, 0)'
            cxt.font = '20px Microsoft YaHei'
            cxt.fillText(self.timer_num, self.x + marginLeft + 30, self.y + 35)
        }
    }
    drawCountDown(){
        let self=this
        self.timer=setInterval(()=>{
            if(self.timer_num>0){
                self.timer_num--
            }else{
                clearInterval(self.timer)
                self.timer_num=self.timer_spacing/1000
            }
        },1000)
    }




    changeState(){
        let self=this
        if(!self.canClick){
            self.timer=setTimeout(()=> {
            self.canClick=true
            },self.timer_spacing)
        }
    }
}







class Car{
    constructor(obj){
        let c={
            img: imageFromPath(allImg.car),
            state:1,
            state_NORMALE:1,
            state_ATTACK:2,
            w:71,
            h:57,
            x:obj.x,
            y:obj.y,
            row:obj.row,
        }
        Object.assign(this,c)

    }
    static new(obj){
        let c=new this(obj)
        return c
    }
    draw(game,cxt){
        let self = this
        self.canMove()
        self.state === self.state_ATTACK && self.step(game)
        cxt.drawImage(self.img, self.x, self.y)
    }
    step(game) {
        game.state === game.state_RUNNING ? this.x += 15 : this.x = this.x
    }
    // 判断是否移动小车 （zombie.x < 150时）
    canMove () {
        let self = this
        for (let zombie of window._main.zombies) {
            if (zombie.row === self.row) {
                if (zombie.x < 150) { 
                self.state = self.state_ATTACK
                }
                if (self.state === self.state_ATTACK) { 
                if (zombie.x - self.x < self.w && zombie.x < 950) {
                    zombie.life = 0
                    zombie.changeAnimation('die')
                }
                }
            }
        }
    }
}

class Bullet{
    constructor(plant){
        let b={
            img: imageFromPath(allImg.bullet),
            w:56,
            h:34,
            x:0,
            y:0,
        }
        Object.assign(this,b)
    }
    static new(plant){
        let b=new this(plant)
        switch (plant.section) {
        case 'peashooter':
            b.x = plant.x + 30
            b.y = plant.y
            break
        case 'repeater':
            b.x = plant.x + 30
            b.y = plant.y
            break
        case 'gatlingpea':
            b.x = plant.x + 30
            b.y = plant.y + 10
            break
        }
        return b
    }
    draw(game,cxt){
        let self=this
        self.step(game)
        cxt.drawImage(self.img,self.x,self.y)
    }
    step(game){
        if(game.state === game.state_RUNNING){
            this.x+=4
        }else{
            this.x=this.x
        }
    }
}












class Animation{
    constructor (role, action, fps) {
        let a = {
        type: role.type,                                   // 动画类型（植物、僵尸等等）
        section: role.section,                             // 植物或者僵尸类别（向日葵、豌豆射手）
        action: action,                                    // 根据传入动作生成不同动画对象数组
        images: [],                                        // 当前引入角色图片对象数组
        img: null,                                         // 当前显示角色图片
        imgIdx: 0,                                         // 当前角色图片序列号
        count: 0,                                          // 计数器，控制动画运行
        imgHead: null,                                     // 当前显示角色头部图片
        imgBody: null,                                     // 当前显示角色身体图片
        imgIdxHead: 0,                                     // 当前角色头部图片序列号
        imgIdxBody: 0,                                     // 当前角色身体图片序列号
        countHead: 0,                                      // 当前角色头部计数器，控制动画运行
        countBody: 0,                                      // 当前角色身体计数器，控制动画运行
        fps: fps,                                          // 角色动画运行速度系数，值越小，速度越快
        }
        Object.assign(this, a)
    }
    // 创建，并初始化当前对象
    static new (role, action, fps) {
        let a = new this(role, action, fps)
        // 濒死动画、死亡动画对象（僵尸）
        if (action === 'dying' || action === 'die') {
        a.images = {
            head: [],
            body: [],
        }
        a.create()
        } else {
        a.create()
        a.images[0].onload = function () {
            role.w = this.width
            role.h = this.height
        }
        }
        return a
    }
    /**
     * 为角色不同动作创造动画序列
     */
    create () {
        let self = this,
            section = self.section    // 植物种类
        switch (self.type) {
        case 'plant':
            for(let i = 0; i < allImg.plants[section][self.action].len; i++){
            let idx = i < 10 ? '0' + i : i,
                path = allImg.plants[section][self.action].path
            // 依次添加动画序列
            self.images.push(imageFromPath(path.replace(/\*/, idx)))
            }
            break
        case 'zombie':
            // 濒死动画、死亡动画对象，包含头部动画以及身体动画
            if (self.action === 'dying' || self.action === 'die') {
            for(let i = 0; i < allImg.zombies[self.action].head.len; i++){
                let idx = i < 10 ? '0' + i : i,
                    path = allImg.zombies[self.action].head.path
                // 依次添加动画序列
                self.images.head.push(imageFromPath(path.replace(/\*/, idx)))
            }
            for(let i = 0; i < allImg.zombies[self.action].body.len; i++){
                let idx = i < 10 ? '0' + i : i,
                    path = allImg.zombies[self.action].body.path
                // 依次添加动画序列
                self.images.body.push(imageFromPath(path.replace(/\*/, idx)))
            }
            } else { // 普通动画对象
            for(let i = 0; i < allImg.zombies[self.action].len; i++){
                let idx = i < 10 ? '0' + i : i,
                    path = allImg.zombies[self.action].path
                // 依次添加动画序列
                self.images.push(imageFromPath(path.replace(/\*/, idx)))
            }
            }
            break
        case 'loading': // loading动画
            for(let i = 0; i < allImg.loading[self.action].len; i++){
            let idx = i < 10 ? '0' + i : i,
                path = allImg.loading[self.action].path
            // 依次添加动画序列
            self.images.push(imageFromPath(path.replace(/\*/, idx)))
            }
            break
        }
    }
}

class Role{
    constructor (obj) {
        let r = {
            id: Math.random().toFixed(6) * Math.pow(10, 6),     
            type: obj.type,                                    
            section: obj.section,                             
            x: obj.x,                                        
            y: obj.y,                                          
            row: obj.row,                                       
            col: obj.col,                                     
            w: 0,                                          
            h: 0,                                         
            isAnimeLenMax: false,                              
            isDel: false,                                        
            isHurt: false,                                      
        }
        Object.assign(this, r)
    }
}




class Plant extends Role{
    constructor (obj) {
        super(obj)
        let p = {
        life: 3,                                            
        idle: null,                                          
        idleH: null,                                         
        idleM: null,                                         
        idleL: null,                                        
        attack: null,                                        
        digest: null,                                        
        bullets: [],                                        
        state: obj.section === 'wallnut' ? 2 : 1,            
        state_IDLE: 1,                                       
        state_IDLE_H: 2,                                   
        state_IDLE_M: 3,                                    
        state_IDLE_L: 4,                                    
        state_ATTACK: 5,                                  
        state_DIGEST: 6,                                   
        canShoot: false,                                    
        canSetTimer: obj.canSetTimer,                    
        sunTimer: null,                                    
        sunTimer_spacing: 10,                             
        }
        Object.assign(this, p)
    }
    static new (obj) {
        let p = new this(obj)
        p.init()
        return p
    }
    setSunTimer () {
        let self = this
        self.sunTimer = setInterval(function () {
        let img = document.createElement('img'),                 
            container = document.getElementsByTagName('body')[0], 
            id = self.id,                                        
            top = self.y + 30,
            left = self.x - 130,
            keyframes1 = [                                        
                { transform: 'translate(0,0)', opacity: 0 },
                { offset: .3,transform: 'translate(0,0)', opacity: 1 },
                { offset: .5,transform: 'translate(0,0)', opacity: 1 },
                { offset: 1,transform: 'translate(-'+ (left - 110) +'px,-'+ (top + 50) +'px)',opacity: 0 }
            ]
        img.src = 'images/sun.gif'
        img.className += 'sun-img plantSun' + id
        img.style.top = top + 'px'
        img.style.left = left + 'px'
        container.appendChild(img)
        let sun = document.getElementsByClassName('plantSun' + id)[0]
        sun.animate(keyframes1,keyframesOptions)
        setTimeout(()=> {
            sun.parentNode.removeChild(sun)
            window._main.sunnum.changeSunNum()
        }, 2700)
        }, self.sunTimer_spacing * 1000)
    }
    clearSunTimer () {
        let self = this
        clearInterval(self.sunTimer)
    }
    init () {
        let self = this,
            setPlantFn = null
        setPlantFn = {
        sunflower () { 
            self.idle = Animation.new(self, 'idle', 12)
            self.canSetTimer && self.setSunTimer()
        },
        peashooter () { 
            self.canShoot = true
            self.idle = Animation.new(self, 'idle', 12)
            self.attack = Animation.new(self, 'attack', 12)
        },
        repeater () { 
            self.canShoot = true
            self.idle = Animation.new(self, 'idle', 12)
            self.attack = Animation.new(self, 'attack', 8)
        },
        gatlingpea () { 
            self.y -= 12
            self.canShoot = true
            self.idle = Animation.new(self, 'idle', 8)
            self.attack = Animation.new(self, 'attack', 4)
        },
        cherrybomb () { 
            self.x -= 15
            self.idle = Animation.new(self, 'idle', 15)
            self.attack = Animation.new(self, 'attack', 15)
            setTimeout(()=> {
            self.state = self.state_ATTACK
            }, 2000)
        },
        wallnut () {
            self.x += 15
            self.life = 12
            self.idleH = Animation.new(self, 'idleH', 10)
            self.idleM = Animation.new(self, 'idleM', 8)
            self.idleL = Animation.new(self, 'idleL', 10)
        },
        chomper () { 
            self.life = 5
            self.y -= 45
            self.idle = Animation.new(self, 'idle', 10)
            self.attack = Animation.new(self, 'attack', 12)
            self.digest = Animation.new(self, 'digest', 12)
        },
        }
        for (let key in setPlantFn) {
        if (self.section === key) {
            setPlantFn[key]()
        }
        }
    }
    draw (cxt) {
        let self = this,
            stateName = self.switchState()
        switch (self.isHurt) {
        case false:
            if (self.section === 'cherrybomb' && self.state === self.state_ATTACK) {
            cxt.drawImage(self[stateName].img, self.x - 60, self.y - 50)
            } else {
            cxt.drawImage(self[stateName].img, self.x, self.y)
            }
            break
        case true:
            cxt.globalAlpha = 0.5
            cxt.beginPath()
            cxt.drawImage(self[stateName].img, self.x, self.y)
            cxt.closePath()
            cxt.save()
            cxt.globalAlpha = 1
            break
        }
    }
    update (game) {
        let self = this,
            section = self.section,
            stateName = self.switchState()
        let animateLen = allImg.plants[section][stateName].len
        self[stateName].count += 1
        self[stateName].imgIdx = Math.floor(self[stateName].count / self[stateName].fps)
        self[stateName].imgIdx === animateLen - 1 ? self[stateName].count = 0 : self[stateName].count = self[stateName].count
        if (game.state === game.state_RUNNING) {
        self[stateName].img = self[stateName].images[self[stateName].imgIdx]
        if (self[stateName].imgIdx === animateLen - 1) {
            if (stateName === 'attack' && !self.isDel) {
            if (self.canShoot) {
                self.shoot()
                self.section === 'repeater' && setTimeout(()=> {self.shoot()}, 250)
            }
            self.section === 'cherrybomb' ? self.isDel = true : self.isDel = false
            if (self.section === 'chomper') {
                setTimeout(()=> {
                self.changeAnimation('digest')
                }, 0)
            }
            } else if (self.section === 'chomper' && stateName === 'digest') {
            setTimeout(()=> {
                self.changeAnimation('idle')
            }, 30000)
            }
            self.isAnimeLenMax = true
        } else {
            self.isAnimeLenMax = false
        }
        }
    }
    canAttack () {
        let self = this
        if (self.section === 'sunflower' || self.section === 'wallnut') return false
        for (let zombie of window._main.zombies) {
        if (self.section === 'cherrybomb') {
            if (Math.abs(self.row - zombie.row) <= 1 && Math.abs(self.col - zombie.col) <= 1 && zombie.col < 10) {
            self.changeAnimation('attack')
            zombie.life = 0
            zombie.changeAnimation('dieboom')
            }
        } else if (self.section === 'chomper' && self.state === self.state_IDLE) { 
            if (self.row === zombie.row && (zombie.col - self.col) <= 1 && zombie.col < 10) {
            self.changeAnimation('attack')
            setTimeout(()=> {
                zombie.isDel = true
            }, 1300)
            }
        } else if (self.canShoot && self.row === zombie.row) { 
            zombie.x < 940 && self.x < zombie.x + 10 && zombie.life > 0 ? self.changeAnimation('attack') : self.changeAnimation('idle')
            if (!self.isDel) {
            self.bullets.forEach(function (bullet, j) {
                if (Math.abs(zombie.x + bullet.w - bullet.x) < 10 && zombie.life > 0) {
                self.bullets.splice(j, 1)
                if (zombie.life !== 0) {
                    zombie.life--
                    zombie.isHurt = true
                    setTimeout(()=> {
                    zombie.isHurt = false
                    }, 200)
                }
                if (zombie.life === 2) {
                    zombie.changeAnimation('dying')
                } else if (zombie.life === 0) {
                    zombie.changeAnimation('die')
                }
                }
            })
            }
        }
        }
    }
    shoot () {
        let self = this
        self.bullets[self.bullets.length] = Bullet.new(self)
    }
    switchState () {
        let self = this,
            state = self.state,
            dictionary = {
            idle: self.state_IDLE,
            idleH: self.state_IDLE_H,
            idleM: self.state_IDLE_M,
            idleL: self.state_IDLE_L,
            attack: self.state_ATTACK,
            digest: self.state_DIGEST,
            }
        for (let key in dictionary) {
        if (state === dictionary[key]) {
            return key
        }
        }
    }
    changeAnimation (action) {
        let self = this,
            stateName = self.switchState(),
            dictionary = {
            idle: self.state_IDLE,
            idleH: self.state_IDLE_H,
            idleM: self.state_IDLE_M,
            idleL: self.state_IDLE_L,
            attack: self.state_ATTACK,
            digest: self.state_DIGEST,
            }
        if (action === stateName) return
        self.state = dictionary[action]
    }
}

class Zombie extends Role{
    constructor(obj){
        super(obj)
        // 僵尸类私有属性
        let z = {
        life: 10,                                            // 角色血量
        canMove: true,                                       // 判断当前角色是否可移动
        attackPlantID: 0,                                    // 当前攻击植物对象 ID
        idle: null,                                          // 站立动画对象
        run: null,                                           // 奔跑动画对象
        attack: null,                                        // 攻击动画对象
        dieboom: null,                                       // 被炸死亡动画对象
        dying: null,                                         // 濒临死亡动画对象
        die: null,                                           // 死亡动画对象
        state: 1,                                            // 保存当前状态值，默认为1
        state_IDLE: 1,                                       // 站立不动状态
        state_RUN: 2,                                        // 奔跑状态
        state_ATTACK: 3,                                     // 攻击状态
        state_DIEBOOM: 4,                                    // 死亡状态
        state_DYING: 5,                                      // 濒临死亡状态
        state_DIE: 6,                                        // 死亡状态
        state_DIGEST: 7,                                     // 消化死亡状态
        speed: 3,                                            // 移动速度
        head_x: 0,                                           // 头部动画 x 轴坐标
        head_y: 0,                                           // 头部动画 y 轴坐标
        }
        Object.assign(this, z)
    }
    static new(obj){
        let p=new this(obj)
        p.init()
        return p
    }
    init () {
        let self = this
        // 站立
        self.idle = Animation.new(self, 'idle', 12)
        // 移动
        self.run = Animation.new(self, 'run', 12)
        // 攻击
        self.attack = Animation.new(self, 'attack', 8)
        // 炸死
        self.dieboom = Animation.new(self, 'dieboom', 8)
        // 濒死
        self.dying = Animation.new(self, 'dying', 8)
        // 死亡
        self.die = Animation.new(self, 'die', 12)
    }
    draw (cxt) {
        let self = this,
            stateName = self.switchState()
        if (stateName !== 'dying' && stateName !== 'die') { // 绘制普通动画
        if (!self.isHurt) { // 未受伤时，绘制正常动画
            cxt.drawImage(self[stateName].img, self.x, self.y)
        } else { // 受伤时，绘制带透明度动画
            // 绘制带透明度动画
            cxt.globalAlpha = 0.5
            cxt.beginPath()
            cxt.drawImage(self[stateName].img, self.x, self.y)
            cxt.closePath()
            cxt.save()
            cxt.globalAlpha = 1
        }
        }else{ // 绘制濒死、死亡动画
            if (!self.isHurt) { // 未受伤时，绘制正常动画
                cxt.drawImage(self[stateName].imgHead, self.head_x + 70, self.head_y - 10)
                cxt.drawImage(self[stateName].imgBody, self.x, self.y)
            }else { // 受伤时，绘制带透明度动画
                // 绘制带透明度身体
                cxt.globalAlpha = 0.5
                cxt.beginPath()
                cxt.drawImage(self[stateName].imgBody, self.x, self.y)
                cxt.closePath()
                cxt.save()
                cxt.globalAlpha = 1
                // 头部不带透明度
                cxt.drawImage(self[stateName].imgHead, self.head_x + 70, self.head_y - 10)
            }
        }
    }


    // 更新状态
    update (game) {
        let self = this,
            stateName = self.switchState()
        // 更新能否移动状态值
        self.canMove ? self.speed = 3 : self.speed = 0
        // 更新僵尸列坐标值
        self.col = Math.floor((self.x - window._main.zombies_info.x) / 80 + 1)
        if (stateName !== 'dying' && stateName !== 'die') { // 普通动画（站立，移动，攻击）
        // 修改当前动画序列长度
        let animateLen = allImg.zombies[stateName].len
        // 累加动画计数器
        self[stateName].count += 1
        // 设置角色动画运行速度
        self[stateName].imgIdx = Math.floor(self[stateName].count / self[stateName].fps)
        // 一整套动画完成后重置动画计数器
        if (self[stateName].imgIdx === animateLen) {
            self[stateName].count = 0
            self[stateName].imgIdx = 0
            if (stateName === 'dieboom') { // 被炸死亡状态
            // 当死亡动画执行完一轮后，移除当前角色
            self.isDel = true
            }
            // 当前动画帧数达到最大值
            self.isAnimeLenMax = true
        } else {
            self.isAnimeLenMax = false
        }
        // 游戏运行状态
        if (game.state === game.state_RUNNING) {
            // 设置当前帧动画对象
            self[stateName].img = self[stateName].images[self[stateName].imgIdx]
            if (stateName === 'run') { // 当僵尸移动时，控制移动速度
            self.x -= self.speed / 17
            }
        }
        } else if (stateName === 'dying') { // 濒死动画，包含两个动画对象
        // 获取当前动画序列长度
        let headAnimateLen = allImg.zombies[stateName].head.len,
            bodyAnimateLen = allImg.zombies[stateName].body.len
        // 累加动画计数器
        if (self[stateName].imgIdxHead !== headAnimateLen - 1) {
            self[stateName].countHead += 1
        }
        self[stateName].countBody += 1
        // 设置角色动画运行速度
        self[stateName].imgIdxHead = Math.floor(self[stateName].countHead / self[stateName].fps)
        self[stateName].imgIdxBody = Math.floor(self[stateName].countBody / self[stateName].fps)
        // 设置当前帧动画对象，头部动画
        if (self[stateName].imgIdxHead === 0) {
            self.head_x = self.x
            self.head_y = self.y
            self[stateName].imgHead = self[stateName].images.head[self[stateName].imgIdxHead]
        } else if (self[stateName].imgIdxHead === headAnimateLen) {
            self[stateName].imgHead = self[stateName].images.head[headAnimateLen - 1]
        } else {
            self[stateName].imgHead = self[stateName].images.head[self[stateName].imgIdxHead]
        }
        // 设置当前帧动画对象，身体动画
        if (self[stateName].imgIdxBody === bodyAnimateLen) {
            self[stateName].countBody = 0
            self[stateName].imgIdxBody = 0
            // 当前动画帧数达到最大值
            self.isAnimeLenMax = true
        } else {
            self.isAnimeLenMax = false
        }
        // 游戏运行状态
        if (game.state === game.state_RUNNING) {
            // 设置当前帧动画对象
            self[stateName].imgBody = self[stateName].images.body[self[stateName].imgIdxBody]
            if (stateName === 'dying') { // 濒死状态，可以移动
            self.x -= self.speed / 17
            }
        }
        } else if (stateName === 'die') { // 死亡动画，包含两个动画对象
        // 获取当前动画序列长度
        let headAnimateLen = allImg.zombies[stateName].head.len,
            bodyAnimateLen = allImg.zombies[stateName].body.len
        // 累加动画计数器
        if (self[stateName].imgIdxBody !== bodyAnimateLen - 1) {
            self[stateName].countBody += 1
        }
        // 设置角色动画运行速度
        self[stateName].imgIdxBody = Math.floor(self[stateName].countBody / self[stateName].fps)
        // 设置当前帧动画对象，死亡状态，定格头部动画
        if (self[stateName].imgIdxHead === 0) {
            if (self.head_x == 0 && self.head_y == 0) {
            self.head_x = self.x
            self.head_y = self.y
            }
            self[stateName].imgHead = self[stateName].images.head[headAnimateLen - 1]
        }
        // 设置当前帧动画对象，身体动画
        if (self[stateName].imgIdxBody === 0) {
            self[stateName].imgBody = self[stateName].images.body[self[stateName].imgIdxBody]
        } else if (self[stateName].imgIdxBody === bodyAnimateLen - 1) {
            // 当死亡动画执行完一轮后，移除当前角色
            self.isDel = true
            self[stateName].imgBody = self[stateName].images.body[bodyAnimateLen - 1]
        } else {
            self[stateName].imgBody = self[stateName].images.body[self[stateName].imgIdxBody]
        }
        }
    }
    // 检测僵尸是否可攻击植物
    canAttack () {
        let self = this
        // 循环植物对象数组
        for (let plant of window._main.plants) {
        if (plant.row === self.row && !plant.isDel) { // 当僵尸和植物处于同行时
            if (self.x - plant.x < -20 && self.x - plant.x > -60) {
            if (self.life > 2) {
                // 保存当前攻击植物 hash 值，在该植物被删除时，再控制当前僵尸移动
                self.attackPlantID !== plant.id ? self.attackPlantID = plant.id : self.attackPlantID = self.attackPlantID
                self.changeAnimation('attack')
            } else {
                self.canMove = false
            }
            if (self.isAnimeLenMax && self.life > 2) {  // 僵尸动画每执行完一轮次
                // 扣除植物血量
                if (plant.life !== 0) {
                plant.life--
                plant.isHurt = true
                setTimeout(()=> {
                    plant.isHurt = false
                    // 坚果墙判断切换动画状态
                    if (plant.life <= 8 && plant.section === 'wallnut') {
                    plant.life <= 4 ? plant.changeAnimation('idleL') : plant.changeAnimation('idleM')
                    }
                    // 判断植物是否可移除
                    if (plant.life <= 0) {
                    // 设置植物死亡状态
                    plant.isDel = true
                    // 清除死亡向日葵的阳光生成定时器
                    plant.section === 'sunflower' && plant.clearSunTimer()
                    }
                }, 200)
                }
            } 
            }
        }
        }
    }








    /**
   * 判断角色状态并返回对应动画对象名称方法
   */
    switchState () {
        let self = this,
            state = self.state,
            dictionary = {
            idle: self.state_IDLE,
            run: self.state_RUN,
            attack: self.state_ATTACK,
            dieboom: self.state_DIEBOOM,
            dying: self.state_DYING,
            die: self.state_DIE,
            digest: self.state_DIGEST,
            }
        for (let key in dictionary) {
        if (state === dictionary[key]) {
            return key
        }
        }
    }
    /**
     * 切换角色动画
     * game => 游戏引擎对象
     * action => 动作类型
     *  -idle: 站立不动
     *  -attack: 攻击
     *  -die: 死亡
     *  -dying: 濒死
     *  -dieboom: 爆炸
     *  -digest: 被消化
     */
    changeAnimation (action) {
        let self = this,
            stateName = self.switchState(),
            dictionary = {
            idle: self.state_IDLE,
            run: self.state_RUN,
            attack: self.state_ATTACK,
            dieboom: self.state_DIEBOOM,
            dying: self.state_DYING,
            die: self.state_DIE,
            digest: self.state_DIGEST,
            }
        if (action === stateName) return
        self.state = dictionary[action]
    }
}










